package com.pengtu.web.project.org;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.dao.QueryFilter.MatchType;
import com.pengtu.entity.app.Organization;
import com.pengtu.service.app.AccountManager;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

/**
 * 单位管理
 * 
 * @author libitum
 * 
 */
@Results({
		@Result(name = BaseActionSupport.RELOAD, type = "redirect", location = "organization.action"),
		@Result(name = BaseActionSupport.LOGIN, type = "redirect", location = "/project/sign/design.action?projectId=${projectId}&contractKind=${typeId}&deptId=${deptId}") })
public class OrganizationAction extends BaseActionSupport<Organization> {

	private static final long serialVersionUID = 1L;

	private AccountManager accountManager;

	// -- 页面属性 --//
	private String id;
	private Organization entity;
	private List<Organization> allOrganizationList;
	private Page<Organization> page = new Page<Organization>(10);// 每页5条记录
	private String typeId;
	private String projectId;
	private String deptId;
	private String signid;

	public String getSignid() {
		return signid;
	}

	public void setSignid(String signid) {
		this.signid = signid;
	}

	@Action("/quality/org/organization")
	public String list() throws Exception {
		QueryFilter filters = QueryFilter.buildFromHttpRequest(Struts2Utils
				.getRequest());
		// 设置默认排序方式
		if (filters == null) {
			allOrganizationList = accountManager.getOrganization();
		}
		if (!page.isOrderBySetted()) {
			page.setOrderBy("id");
			page.setOrder(Page.DESC);
		}
		page = accountManager.searchOrganization(page, filters);
		return SUCCESS;
	}

	@Action("/account/api/organization")
	public String apilist() throws Exception {
		QueryFilter filters = QueryFilter.buildFromHttpRequest(Struts2Utils
				.getRequest());

		// 设置默认排序方式
		if (filters == null) {
			allOrganizationList = accountManager.getOrganization();
		}
		if (!page.isOrderBySetted()) {
			page.setOrderBy("id");
			page.setOrder(Page.DESC);
		}
		page = accountManager.searchOrganization(page, filters);
		return SUCCESS;
	}

	@Action("/project/sign/org/organization")
	public String signlist() throws Exception {

		// 获得文本框输入值
		String orgName = Struts2Utils.getParameter("q");
		QueryFilter queryfilter = QueryFilter.buildFromHttpRequest(Struts2Utils
				.getRequest());
		// 按照类型查找
		queryfilter.addFilter("type", typeId,MatchType.EQ);
		if(StringUtils.isNotBlank(orgName)){
			queryfilter.addFilter("orgName", orgName);
		}
		// 从数据库获取符合条件的数据
		page = new Page<Organization>(10);
		Page<Organization> orgPage =  accountManager.searchOrganization(page, queryfilter); 
		List<Organization> orgList = new ArrayList<Organization>();
		if(null!=orgPage){
			 orgList = orgPage.getResult();
		}
		// 将数据用Json串保存
		
		String htmlStr = "";
		if(null!=orgList){
			if(orgList.size()>0){
				htmlStr= "<h3>查询到"+orgList.size()+"条单位名称为: 【"+orgName+"】 的单位信息！</h3><ol>";
				for (int i = 0; i < orgList.size(); i++) {
					htmlStr += "<li><a href = \" #\" onclick=\" setValue('"+orgList.get(i).getOrgName()+"','"+orgList.get(i).getId()+"'); \"> <font colour = \"yellow\"> "+orgList.get(i).getOrgName()+"</font></a></li>";
				}
				htmlStr += "<li><a href=\"#\" onclick=\"addOrg();\">【新增】</a>;</li>";
			}else{
				htmlStr = "<h3>没有查询到单位名称为'【"+orgName+"】'的单位信息，建议您：</h3><ol>";
				htmlStr += "<li>1 : 修改关键字,重新查询;</li>";
				htmlStr += "<li>2 : 选择 <a href=\"#\" onclick=\"addOrg();\">【新增】</a>，添加单位信息;</li>";
			}
		}
		Struts2Utils.renderText(htmlStr+="</li></ol>");
		return NONE;
	}

	@Override
	public String input() throws Exception {
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		accountManager.saveOrganization(entity);
		String mes = "";
		if (StringUtils.isNotEmpty(id)) {
			mes = "修改";
		} else {
			mes = "新增";
		}
		addActionMessage(mes + "单位成功");
		id = entity.getId();
		Struts2Utils.sendRedirectUrl("/project/org/organization!input.action?id="+id);
		return NONE;
	}

	/**
	 * 验证机构代码是否重复
	 */
	public void checkOrgNum() {
		// TODO Auto-generated method stub
		String orgNum = Struts2Utils.getParameter("orgNum");
		String oldOrgNum = Struts2Utils.getParameter("oldOrgNum");
		if (accountManager.isOrgNumUnique(orgNum, oldOrgNum)) {
			Struts2Utils.renderText("true");
		} else {
			Struts2Utils.renderText("false");
		}
	}

	/**
	 * 验证单位名是否重复
	 */
	public void checkOrgName() {
		// TODO Auto-generated method stub
		String orgName = Struts2Utils.getParameter("orgName");
		String oldOrgName = Struts2Utils.getParameter("oldOrgName");
		if (accountManager.isOrgNameUnique(orgName, oldOrgName)) {
			Struts2Utils.renderText("true");
		} else {
			Struts2Utils.renderText("false");
		}
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		accountManager.deleteOrganization(id);
		addActionMessage("删除单位信息");
		return RELOAD;
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		if (StringUtils.isNotEmpty(id)) {
			entity = accountManager.getOrganization(id);
		} else {
			entity = new Organization();
		}
	}

	public String getTypeId() {
		return typeId;
	}

	public void setTypeId(String typeId) {
		this.typeId = typeId;
	}

	public AccountManager getAccountManager() {
		return accountManager;
	}

	public void setAccountManager(AccountManager accountManager) {
		this.accountManager = accountManager;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Organization getEntity() {
		return entity;
	}

	public void setEntity(Organization entity) {
		this.entity = entity;
	}

	public List<Organization> getAllOrganizationList() {
		return allOrganizationList;
	}

	public Page<Organization> getPage() {
		return page;
	}

	public void setPage(Page<Organization> page) {
		this.page = page;
	}

	public String getProjectId() {
		return projectId;
	}

	public void setProjectId(String projectId) {
		this.projectId = projectId;
	}

	public String getDeptId() {
		return deptId;
	}

	public void setDeptId(String deptId) {
		this.deptId = deptId;
	}

	@Override
	public Organization getModel() {
		return entity;
	}

}
